﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Hont.UMotionGraph
{
    public class MGCloneContainer : MoGraphTransformContainer
    {
        public enum CloneTypeEnum { None, Cube, Sphere };
        public enum CloneModeEnum { None, Object };
        public CloneModeEnum cloneMode;
        public GameObject cloneModeReferenceObject;
        public CloneTypeEnum cloneType;
        public GameObject customTemplate;
        public float cloneCount;
        public Vector3 cloneSpacing;
        GameObject mTemplate;
        float mLastCloneCount = -1;
        List<GameObject> mCloneObjectList;


        protected override void Awake()
        {
            base.Awake();

            mCloneObjectList = new List<GameObject>();

            switch (cloneType)
            {
                case CloneTypeEnum.None:
                    mTemplate = customTemplate;
                    break;
                case CloneTypeEnum.Cube:
                    mTemplate = GameObject.CreatePrimitive(PrimitiveType.Cube);
                    break;
                case CloneTypeEnum.Sphere:
                    mTemplate = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                    break;
            }
            mTemplate.transform.parent = transform;
        }

        protected override void Update()
        {
            CloneTypeUpdate();
            CloneModeUpdate();

            base.Update();
        }

        public override Vector3 GetValue(int type, int inx)
        {
            return mSourceValueArr[inx];
        }

        public override void UpdateValue(Vector3[] values)
        {
            for (int i = 0; i < values.Length; i++)
            {
                elements[i].position = values[i];
            }
        }

        void CloneTypeUpdate()
        {
            if (mLastCloneCount != cloneCount)
            {
                for (int i = 0; i < mCloneObjectList.Count; i++)
                {
                    Destroy(mCloneObjectList[i]);
                }
                mCloneObjectList.Clear();

                for (int i = 0; i < cloneCount; i++)
                {
                    var instanced = Instantiate(mTemplate);
                    instanced.transform.parent = mTemplate.transform;
                    instanced.transform.localPosition = Vector3.zero;
                    instanced.transform.localRotation = Quaternion.identity;
                    instanced.transform.localScale = Vector3.one;

                    mCloneObjectList.Add(instanced);
                }

                elements = mCloneObjectList.ConvertAll(m => m.transform).ToArray();
                mUpdateMiddleValueArr = new Vector3[elements.Length];
                mSourceValueArr = new Vector3[elements.Length];
                for (int i = 0; i < mCloneObjectList.Count; i++)
                {
                    mSourceValueArr[i] = mCloneObjectList[i].transform.position;
                }

                mEffectorList.ForEach(m => m.Init(this));
            }

            mLastCloneCount = cloneCount;
        }

        void CloneModeUpdate()
        {
            if (cloneMode == CloneModeEnum.Object)
            {
                var mesh = cloneModeReferenceObject.GetComponent<MeshFilter>();

                if (mesh != null)
                {
                    cloneCount = mesh.sharedMesh.vertexCount;
                    CloneTypeUpdate();

                    for (int i = 0; i < elements.Length; i++)
                    {
                        elements[i].position = cloneModeReferenceObject.transform.position + mesh.sharedMesh.vertices[i];
                        mSourceValueArr[i] = elements[i].position;
                    }
                }
            }
            else
            {
                for (int i = 0; i < cloneCount; i++)
                {
                    mCloneObjectList[i].transform.position = transform.position + i * cloneSpacing;
                    mSourceValueArr[i] = mCloneObjectList[i].transform.position;
                }
            }
        }
    }
}
